<!-- 系统 菜单管理 -->
<template>
  <div class="sysMenu">
    <!-- 行内表单 开始 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-button
          type="primary"
          size="medium"
          @click="dialogVisible = true"
          v-if="hasAuth('sys:menu:save')"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 行内表单 结束 -->
    <!-- tree表格 开始 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      stripe
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="名称" width="240"> </el-table-column>
      <el-table-column prop="perms" label="权限编码" width="240">
      </el-table-column>
      <el-table-column prop="icon" label="图标"> </el-table-column>
      <el-table-column prop="type" label="类型" width="70">
        <template slot-scope="scope">
          <el-tag size="medium" v-if="scope.row.type === 0" type="primary"
            >目录</el-tag
          >
          <el-tag size="medium" v-if="scope.row.type === 1" type="success"
            >菜单</el-tag
          >
          <el-tag size="medium" v-if="scope.row.type === 2" type="info"
            >按钮</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="path" label="菜单路径"> </el-table-column>
      <el-table-column prop="component" label="菜单组件"> </el-table-column>
      <el-table-column prop="orderNum" label="排序号" width="70">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="70">
        <template slot-scope="scope">
          <el-tag size="medium" v-if="scope.row.status === 1" type="success"
            >正常</el-tag
          >
          <el-tag size="medium" v-if="scope.row.status === 0" type="danger"
            >禁用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="190px">
        <template slot-scope="scope">
          <!-- 修改 -->

          <el-button
            class="FormButton"
            size="mini"
            icon="el-icon-edit"
            @click="editForm(scope.row.id)"
            v-if="hasAuth('sys:menu:update')"
            >修改</el-button
          >
          <!-- 删除 -->
          <el-button
            class="FormButton"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            v-if="hasAuth('sys:menu:delete')"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- tree表格 结束 -->
    <!-- dialog -->
    <el-dialog
      title="新增菜单"
      :visible.sync="dialogVisible"
      width="30%"
      @closed="FormDialogOnClose('form')"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="editFormRules"
        label-width="80px"
      >
        <el-form-item label="上级菜单" prop="parentId">
          <!-- 模拟树形下拉框 -->
          <el-select v-model="form.parentId" placeholder="请选择上级菜单">
            <template v-for="item in tableData">
              <el-option :label="item.name" :value="item.id" :key="item.id">
              </el-option>
              <template v-for="child in item.children">
                <el-option
                  :label="child.name"
                  :value="child.id"
                  :key="child.id"
                >
                  <span>{{ "- " + child.name }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="perms">
          <el-input v-model="form.perms"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径">
          <el-input v-model="form.path"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件">
          <el-input v-model="form.component"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type" prop="type">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status" prop="status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- dialog -->
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import menuApi from '@/api/menu.js'; //菜单管理api
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      //弹框弹出标识
      dialogVisible: false,
      // 新增/修改 表单
      form: {
        id: null,
        type: 0,
        status: 1,
        orderNum: 1,
      },
      //新增 修改表单规则
      editFormRules: {
        parentId: [
          { required: true, message: "请选择上级菜单", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        perms: [{ required: true, message: "请输入权限编码", trigger: "blur" }],
        type: [{ required: true, message: "请选择状态", trigger: "blur" }],
        orderNum: [
          { required: true, message: "请填入排序号", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
      },
      //table数据
      tableData: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //获取菜单列表
    getMenuList() {
      //console.log("loading...")
      menuApi.getList().then((res) => {
        this.tableData = res.data.data;
      });
    },
    //编辑
    editForm(rowId) {
      //调用后台请求，根据id查询单条数据，作页面回显
      menuApi.getInfo(rowId).then((res) => {
        //数据绑定
        this.form = res.data.data.menu;
        this.form.parentName = (res.data.data.parentName);
        //console.log(this.form);
        //打开表单弹框
        this.dialogVisible = true;
      });
    },
    //删除
    removeDataById(rowId) {
      //弹框提示 是否删除 调用api发送删除请求 删除 否 不进行任何操作
      this.$confirm("此操作将删除该列信息, 是否继续删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          menuApi.delete(rowId)
            .then((res) => {
              //提示
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //异步刷新
              this.getMenuList();
            })
            .catch(() => {
              this.$message({
                type: "warning",
                message: "删除失败！",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //表单提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //发送请求，根据form中的id值判断是新增还是修改

          //新增
          if (!this.form.id) {
            menuApi.save(this.form).then((res) => {
              this.$message.success("新增成功！");
              //关闭弹窗自动重置表单内容
              this.dialogVisible = false;
              //重新加载列表
              this.getMenuList();
            });
          }
          //修改
          else if (this.form.id) {
            menuApi.update(this.form).then((res) => {
              this.$message.success("修改成功！");
              //关闭弹窗自动重置表单内容
              this.dialogVisible = false;
              //重新加载列表
              this.getMenuList();
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //表单关闭
    FormDialogOnClose(formName) {
      //console.log("close");
      //清除校验规则
      this.$refs[formName].resetFields();
      //清除表单data
      this.form = {
        type: 0,
        status: 1,
        orderNum: 1,
      };
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    //页面初始化异步请求加载列表
    this.getMenuList();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  //3.0版本弃用 beforeDestroy() {}, //生命周期 - 销毁之前
  //3.0版本弃用 destroyed() {}, //生命周期 - 销毁完成
  beforeUnmount() {}, //生命周期 - 销毁之前
  unmounted() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
/* 表单内按钮 */
.FormButton {
  margin: 0px 5px;
  width: 40%vw;
}
</style>